package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.ModeratorCategory;

public class ModeratorCategoryDao implements BaseDao {

	private static final String GET_BY_USER_ID = "SELECT * FROM moderator_category WHERE user_id = ?";
	private final String ADD_MODERATOR_CATEGORY = "INSERT INTO moderator_category (user_id, category_id) VALUES (?, ?)";
	private final String REMOVE_MODERATOR_CATEGORY = "DELETE FROM moderator_category WHERE id = ?";
	private final String REMOVE_MODERATOR_CATEGORY_BY_USER_ID = "DELETE FROM moderator_category WHERE user_id = ?";
	private final String GET_BY_ID = "SELECT * FROM moderator_category WHERE id = ?";
	private final String UPDATE_MODERATOR_CATEGORY = "UPDATE moderator_category SET user_id=?, category_id=?";
	private final String GET_CATEGORY = "SELECT * FROM moderator_category WHERE user_id = ?";
	private static final Logger LOG = Logger.getLogger("stdout");

	@Override
	public <T> void create(T element) {
		ModeratorCategory temp = (ModeratorCategory) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_MODERATOR_CATEGORY)) {
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getCategoryId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		ModeratorCategory moderatorCategory = null;
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(GET_BY_ID)) {
			preparedStatement.setInt(1, id);
			ResultSet rs = preparedStatement.executeQuery();
			if (rs.next()) {
				moderatorCategory = Converter
						.<ModeratorCategory> fromResaultSetToObject(rs,
								ModeratorCategory.class);
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

		return (T) moderatorCategory;
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_MODERATOR_CATEGORY)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	public void deleteByUserId(Integer userId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_MODERATOR_CATEGORY_BY_USER_ID)) {
			preparedStatement.setInt(1, userId);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}
	
	@Override
	public <T> void update(T element) {
		ModeratorCategory moderatorCategory = (ModeratorCategory) element;
		Connection connection = ConnectionManager.getInstance().getConnection();

		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_MODERATOR_CATEGORY)) {
			preparedStatement.setInt(1, moderatorCategory.getUserId());
			preparedStatement.setInt(2, moderatorCategory.getCategoryId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T getModeratorCategory(Integer moderatorId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<ModeratorCategory> categories = new ArrayList<ModeratorCategory>();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(GET_CATEGORY)) {
			preparedStatement.setInt(1, moderatorId);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				categories.add(Converter
						.<ModeratorCategory> fromResaultSetToObject(rs,
								ModeratorCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

		return (T) categories;
	}

	public List<ModeratorCategory> getModeratorCategories(Integer userId) {
		List<ModeratorCategory> list = new ArrayList<>();
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement statement = connection
				.prepareStatement(GET_BY_USER_ID)) {
			statement.setInt(1, userId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				list.add(Converter.<ModeratorCategory> fromResaultSetToObject(
						rs, ModeratorCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return list;
	}

}
